package com.twu.biblioteca;


import java.util.Scanner;

public class BibliotecaApp {
    BibliotecaApp() {
    }

    public final String[] header = new String[]{"id","name","author","year","status"};
    public String[][] books = new String[][]{
            {"00000001","C++ Primer","Stanley B. Lippman","2005",""},
            {"00000002","Think in Java","Bruce Eckel","2002",""},
            {"00000003","Ruby on Rails","Bruce Tate","2006",""},
            {"00000004","Refactoring Improving the Design of Existing Code","Martin Fowler","1999",""}};

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        bibliotecaApp.run();
    }

    public void run() {
        welcome();
        command();
    }

    public void command() {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        while(scanner.hasNext()) {
            String command = scanner.next();
            switch(command) {
                case "":
                    break;
                case "1":
                case "l":
                case "list":
                    listBook();
                    break;
                case "2":
                case "c":
                case "checkoutBook":
                    checkoutBook();
                    break;
                case "3":
                case "r":
                case "return":
                    returnBook();
                    break;
                case "0":
                case "q":
                case "quit":
                    System.out.println("bye");
                    System.exit(0);
                    break;
                default:
                    help();
                    System.out.println("Select a valid option!");
                    break;
            }
        }
    }

    public void help() {
        System.out.println("1.List - book list");
        System.out.println("2.Checkout - checkoutBook book");
        System.out.println("3.return - return book");
        System.out.println("0.Quit - quit system");
    }

    public void checkoutBook() {
        System.out.print("id? ");
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        if (scanner.hasNext()) {
            String id = scanner.next();
            for(String[] book:books) {
                if(book[0].equals(id) && book[4].equals("")) {
                    book[4] = "checkedOut";
                    System.out.println("Thank you! Enjoy the book.");
                    return;
                }
            }
            System.out.println("That book is not available.");
        }
    }

    public void returnBook() {
        System.out.print("id? ");
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        if (scanner.hasNext()) {
            String id = scanner.next();
            for(String[] book:books) {
                if(book[0].equals(id) && !book[4].equals("")) {
                    book[4] = "";
                    System.out.println("Thank you for returning the book.");
                    return;
                }
            }
            System.out.println("That is not a valid book to return.");
        }
    }

    public void listBook() {
        final int maxWidth = 25;
        int[] rows = rows(maxWidth);
        final String[] masks = mask(rows, " ");
        final String[] masks_ = mask(rows, "_");
        printItems(header, masks_);
        for(String[] book : books) {
            if(book[4].equals(""))
                printItems(book, masks);
        }

    }

    public void printItems(String[] items, String[] masks) {
        int i=0;
        for(String item:items) {
            StringBuilder buff= new StringBuilder(masks[i]);
            buff.replace(0, item.length(), item);
            int row = masks[i].length();
            if(item.length()> row) {
                buff.setLength(row-3);
                System.out.print(buff+"...|");
            } else {
                buff.setLength(row);
                System.out.print(buff+"|");
            }
            ++i;
        }
        System.out.println("");
    }


    public String[] mask(int[] rows, String mask) {
        String[] masks = new String[rows.length];
        int i=0;
        for(int row:rows) {
            String space = "";
            for(int j=0;j<row;++j) {
                space += mask;
            }
            space = space.substring(0,row);
            masks[i++] = space;
        }
        return masks;
    }

    public int[] rows(final int maxWidth) {
        int[] rows = new int[header.length];
        {
            int i=0;
            for (String head : header) {
                if (head.length() > rows[i]) {
                    rows[i] = head.length() < maxWidth ? head.length() : maxWidth;
                }
                ++i;
            }
        }
        for(String[] book : books) {
            int i=0;
            for (String value:book) {
                if(value.length()>rows[i]) {
                    rows[i] = value.length() < maxWidth ? value.length() : maxWidth;
                }
                ++i;
            }
        }
        return rows;
    }

    public void welcome() {
        System.out.println("Welcome Biblioteca!");
    }
}
